package com.neiquan.backstage.event.action;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neiquan.backstage.common.util.CommonResp;
import com.neiquan.backstage.common.util.ExtraSpringHibernateTemplate;
import com.neiquan.backstage.common.util.Paging;
import com.neiquan.backstage.common.util.Sessions;
import com.neiquan.backstage.event.EventProposerService;
import com.neiquan.backstage.event.EventService;
import com.neiquan.backstage.pub.bsc.dao.po.AdminUser;
import com.neiquan.backstage.pub.bsc.dao.po.Event;
import com.neiquan.backstage.pub.bsc.dao.po.EventProposer;

@Controller
@RequestMapping(value = "eventProposerAcion", produces = { "text/json;charset=UTF-8" })
public class EventProposerAction {
	
	private static Class<?> pocAE = EventProposer.class;
	private static Class<?> pocE = Event.class;
	@Autowired
	private EventProposerService eventProposerService;
	
	@Autowired
	private EventService eventService;
	
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	//查询活动报名人
	
	//分页查询
	@ResponseBody
	@RequestMapping(value={"list"})
	public String list(int pageIndex, int pageSize,HttpSession session){
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		List<EventProposer> queryByMemberId = eventProposerService.queryByMemberId(memberId);
		List<Object> list=new ArrayList<>();
		for(EventProposer ev:queryByMemberId){
			Map<String ,Object> map=new HashMap<>();
			map.put("eventProposer", ev);
			Event queryById = eventService.queryById(ev.getEventId());
			map.put("eventName", queryById.getName());
			list.add(map);
		}
		return CommonResp.buildSuccessResp(list).toJsonString();
	} 
//	条件查询报名活动人
	@ResponseBody
	@RequestMapping(value = "search")
	public String search(String eventName) throws ParseException  {
		List<Criterion> criterions = new ArrayList<>();
		if (StringUtils.isNoneBlank(eventName)) {
			criterions.add(Restrictions.like("name", "%" + eventName + "%"));
		}
		Criterion criterion = Restrictions.and(criterions.toArray(new Criterion[] {}));
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(pocE, Order.desc("eventId"),
				criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(pocE, criterion);
		return CommonResp.buildSuccessResp().toJsonString();
	}
}
